﻿using AutoMapper;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;

namespace DataMapper
{
    class Program
    {
        static void Main(string[] args)
        {
            //sqlsugar
            SqlSugarClient db = new SqlSugarClient(
                    new ConnectionConfig()
                    {
                        ConnectionString = "server=47.112.202.179;uid=user_x;pwd=E2tji8KMjc37BAF;database=DiDi",
                        DbType = DbType.SqlServer,//设置数据库类型
                        IsAutoCloseConnection = true,//自动释放数据务，如果存在事务，在事务结束后释放
                        InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息
                    });
           
            //automapper
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<fromData, toData>();
            });
            configuration.AssertConfigurationIsValid();
            var mapper = configuration.CreateMapper();

            //本地测试
            Console.WriteLine("-----------有值参数-----------");
            var tong0 = new fromData()
            {
                Name = "tang",
                Sex = "男",
                Age = 30
            };

            Hashtable ht = new Hashtable();
            ht.Add("Name", "tang");
            ht.Add("Sex", "男");
            ht.Add("Age", 30);

            GetWhere getWhere0 = new GetWhere();
            // var sql0 = getWhere0.AddToSql<fromData>(tong0);
            var sql0 = getWhere0.GetSql(ht);
            Console.WriteLine(sql0);
            Console.WriteLine("-----------忽略值为0的参数-----------");
            var tong2 = new fromData()
            {
                Name = "tang",
                Sex = "男",
                Age = 0
            };
            GetWhere getWhere2 = new GetWhere();
            var sql2 = getWhere2.GetSql<fromData>(tong2);
            Console.WriteLine(sql2);
            Console.WriteLine("-----------sql注入测试-----------");
            var tong = new fromData()
            {
                Name = "0' or 1=1  '",
                Sex = "0",
                Age = 0
            };
            GetWhere getWhere = new GetWhere();
            var sql = getWhere.GetSql<fromData>(tong);
            Console.WriteLine(sql);

            //连接数据库测试
            Console.WriteLine("-----------------WorkFlow表----------------");

            //拿到where条件
            //WF_FlowInstance wF_FlowInstance = new WF_FlowInstance()
            //{
            //    FlowName = "自营订单流程"
            //};
            Hashtable ht3 = new Hashtable();
            ht3.Add("FlowName", "");

            //var sql3 = getWhere.AddToSql<WF_FlowInstance>(wF_FlowInstance);
            var sql3 = getWhere.GetSql(ht3);

            //从数据库中取数据
            var data= db.Queryable<WF_FlowInstance>().Where(sql3).Where(it=>it.FlowInstanceID== "fid_0010d601fc42cfd4144a1300").Select(f => new
            {
                f.FlowInstanceID,
                f.WorkFlowID,
                f.FlowName
            }).Take(2).ToList();
            Console.WriteLine(JsonConvert.SerializeObject(data));

            var fooDto = mapper.Map<toData>(tong);

            //Console.WriteLine(fooDto.Name);
            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }

        public class fromData
        {
            public string Name { get; set; }
            public string Sex { get; set; }

            public int Age { get; set; }
        }

        public class toData
        {
            public string Name { get; set; }
            public string Sex { get; set; }

            public int Age { get; set; }
        }
    }
}
